package com.ejd.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.Authenticator;
import java.net.HttpURLConnection;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebPageData implements Serializable {
    private boolean useProxy = false;
    private String host ;
    private int port;
    private String username;
    private String password;
	public WebPageData() {
		
	}
	
	public WebPageData(boolean useProxy,int port, String host, String username,String password ) {
		this.useProxy = useProxy;
		this.port = port;
		this.host = host;
		this.username = username;
		this.password = password;
	}

	public boolean isUseProxy() {
		return useProxy;
	}

	public void setUseProxy(boolean useProxy) {
		this.useProxy = useProxy;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	private static void initProxy(String host, int port, final String username,   
			  
	            final String password) {   
	  
	        Authenticator.setDefault(new Authenticator() {   
	  
	            protected PasswordAuthentication getPasswordAuthentication() {   
	  
	                return new PasswordAuthentication(username,   
	  
	                        new String(password).toCharArray());   
	  
	            }   
	  
	        });   
	  
	        System.setProperty("http.proxyType", "4");   
	  
	        System.setProperty("http.proxyPort", Integer.toString(port));   
	  
	        System.setProperty("http.proxyHost", host);   
	  
	        System.setProperty("http.proxySet", "true");   
	  
	    }
	
	public String sendUrl(String urlStr, String param) throws Exception {
        StringBuilder tempStr = null;
        HttpURLConnection url_con = null;
        try {
        	String proxy = "yourProxy";
        	int port =8080;
			String username ="username";
			String password ="password";
            URL url = new URL(urlStr);
            if (this.isUseProxy()) {
            	initProxy(this.getHost(), this.getPort(), this.getUsername(), this.getPassword());
            } else {
            	initProxy(proxy,port,username,password);
            }
            url_con = (HttpURLConnection) url.openConnection();
            url_con.setRequestMethod("POST");
            url_con.setDoOutput(true);
            if (null != param) {
            	url_con.getOutputStream().write(param.getBytes());
            }
            url_con.getOutputStream().flush();
            url_con.getOutputStream().close();
            url_con.setReadTimeout(90000);
            InputStream in  = null;
            try {
            	in= url_con.getInputStream();
            } catch (IOException e) {
            	
            }
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            tempStr = new StringBuilder();
            while (rd.read() != -1) {
                tempStr.append(rd.readLine());
            }
        } catch (Exception e) {
        	
        } finally {
            if (url_con != null)
                url_con.disconnect();
        }
        return null == tempStr?"":new String(tempStr);
    }
	public URL sendUrl33(String urlStr, String param) throws Exception {
        StringBuilder tempStr = null;
        HttpURLConnection url_con = null;
        URL url = new URL(urlStr);
        try {
        	String proxy = "yourProxy";
        	int port =8080;
			String username ="username";
			String password ="password";
            if (this.isUseProxy()) {
            	initProxy(this.getHost(), this.getPort(), this.getUsername(), this.getPassword());
            } else {
            	initProxy(proxy,port,username,password);
            }
            url_con = (HttpURLConnection) url.openConnection();
            url_con.setRequestMethod("POST");
            url_con.setDoOutput(true);
            if (null != param) {
            	url_con.getOutputStream().write(param.getBytes());
            }
            url_con.getOutputStream().flush();
            url_con.getOutputStream().close();
            url_con.setReadTimeout(90000);
            InputStream in  = null;
            try {
            	in= url_con.getInputStream();
            } catch (IOException e) {
            	
            }
            BufferedReader rd = new BufferedReader(new InputStreamReader(in));
            tempStr = new StringBuilder();
            while (rd.read() != -1) {
                tempStr.append(rd.readLine());
            }
        } catch (Exception e) {
        	
        } finally {
            if (url_con != null)
                url_con.disconnect();
        }
        return url;
    }
}
